package org.desktop.web.client.custom.widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.HasMouseOutHandlers;
import com.google.gwt.event.dom.client.HasMouseOverHandlers;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.Event;
import com.sencha.gxt.core.client.dom.XDOM;
import com.sencha.gxt.core.client.dom.XElement;
import com.sencha.gxt.widget.core.client.Component;
import com.sencha.gxt.widget.core.client.Window;

public class DesktopFolder extends Component implements HasClickHandlers,
		HasMouseOverHandlers, HasMouseOutHandlers {

	private String text;
	private Window theWindow;

	public enum Icon {
		FOLDER, TIMESHEET, PROJECTS, ROLES, USERS;
	}

	public interface Appearance {

		void render(SafeHtmlBuilder builder, Icon icon, String text);

	}

	// public DesktopFolder(String text) {
	// this(text,
	// GWT.<DesktopFolderDefaultAppearance>
	// create(DesktopFolderDefaultAppearance.class),Icon.FOLDER,);
	// }
	public DesktopFolder(String text, Icon icon, Window aWindow) {
		this(
				text,
				GWT.<DesktopFolderDefaultAppearance> create(DesktopFolderDefaultAppearance.class),
				icon, aWindow);
	}

	public DesktopFolder(String text,
			DesktopFolderDefaultAppearance appearance, Icon icon, Window aWindow) {
		SafeHtmlBuilder builder = new SafeHtmlBuilder();
		this.text = text;
		this.theWindow = aWindow;
		appearance.render(builder, icon, text);

		XElement element = XDOM.create(builder.toSafeHtml());
		setElement((Element) element); // TODO Auto-generated constructor stub

		sinkEvents(Event.BUTTON_RIGHT | Event.ONDBLCLICK);

		setContextMenu(new FolderMenu(this));
	}

	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		// TODO Auto-generated method stub
		return addDomHandler(handler, ClickEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		// TODO Auto-generated method stub
		return addDomHandler(handler, MouseOverEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		// TODO Auto-generated method stub
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public void onBrowserEvent(Event event) {
		// TODO Auto-generated method stub
		super.onBrowserEvent(event);
		switch (event.getTypeInt()) {
		case Event.ONDBLCLICK:
			if (!((AbstractWindow) theWindow).isOpen()) {
				openFolder();
			}
			break;
		}
	}

	public void openFolder() {
		((AbstractWindow) theWindow).init();
		theWindow.setHeaderVisible(true);
		theWindow.setHeadingText(this.text);
		theWindow.setActive(true);
		theWindow.setTitle(this.text);

		theWindow.show();
	}

}
